﻿<%@ WebHandler Language="C#" Class="vacationApply" %>

using System;
using System.Web;
using System.Text;
using Chenxin.Lib.Utility;

//****************************************************************
//   臣信二手房
//   Copyright (c) 2010 Chenxin.                        
//****************************************************************
//****************************************************************      
//作成者       :  he.jifeng   
//创建日期     :  2010-03-24                
//確認者       :  確認者          
//功能概要　   :  休假申请
//****************************************************************
public class vacationApply : AjaxHandlerBase
{ 
    //构造函数
    public vacationApply()
    {
        //休假申请
        htTypePermit.Add("add", Constants.Permit_VacationApply);
        //休假编辑
        htTypePermit.Add("edit", Constants.Permit_VacationEdit);
        //假期删除
        htTypePermit.Add("del", Constants.Permit_VacationDel);
    }
        
    override public void RequestProcess(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        //处理类型
        string strDealType = context.Request.Params["op"];
        //响应字符串
        string strRet = "";
        
        switch (strDealType)
        {
            case "Search":
                strRet = getVacList(context);
                break;
            case "Add":
                strRet = addVac(context);
                break;
            case "Edit":
                strRet = editVac(context);
                break;
            case "Del":
                strRet = delVal(context);
                break;
            case "ChkAnnualVac":
                strRet = checkAnnnualVac(context);
                break;
            case "getCntDepars":
                strRet = getControlDeparts(context);
                break;
        }
        //响应
        context.Response.Write(strRet);
    }

    //*******************************************
    //功能概要       ：获得假期一览
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：获得假期一览
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-22
    //*******************************************
    private string getVacList(HttpContext context)
    {
        //参数列表
        System.Collections.Hashtable htParams = new System.Collections.Hashtable();
        //当前参数
        string strParam;
        //一页所显示的条数
        int intShowRows = 0;
        //当前页码
        int intCurPage = 0;
        //总记录数
        //int intTotalCnt = 0;
        //返回数据
        string strRet;

        string strYearMonth = context.Request.Params["YearMonth"];

        if (!string.IsNullOrEmpty(context.Request["userid"]))
        {
            htParams.Add("UserId", context.Request["userid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request["departid"]))
        {
            htParams.Add("DepartId", context.Request["departid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request["areaid"]))
        {
            htParams.Add("AreaId", context.Request["areaid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request["companyid"]))
        {
            htParams.Add("CompanyId", context.Request["companyid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request["cityid"]))
        {
            htParams.Add("CityId", context.Request["cityid"]);
        }
        //默认检索条件
        else
        {
            htParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
        }
        
        if (strYearMonth != null)
        {
            htParams.Add("Year", strYearMonth.Substring(0,4));
            htParams.Add("Month", strYearMonth.Substring(5, 2));
        }
        strParam = context.Request.Params["pageindex"];
        if (strParam != null & strParam != "")
        {
            intCurPage = Convert.ToInt32(strParam) + 1;
            htParams.Add("page", intCurPage.ToString());
        }
        strParam = context.Request.Params["pagesize"];
        if (strParam != null & strParam != "")
        {
            intShowRows = Convert.ToInt32(strParam);
            htParams.Add("rows", strParam);
        }
        //排序项目
        htParams.Add("sidx", "DateTime");

        //权限判断
        //查看所有权限
        if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllVacation))
        {
            htParams.Add("Permit", Constants.Permit_ViewAllVacation);
        }
        //查看城市权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCityVacation))
        {
            htParams.Add("Permit", Constants.Permit_ViewCityVacation);
            htParams.Add("CurCityId", CurrentUserInfo.CityId);
        }
        //查看公司权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCompanyVacation))
        {
            htParams.Add("Permit", Constants.Permit_ViewCompanyVacation);
            htParams.Add("CurCompanyId", CurrentUserInfo.CompanyId);
        }
        //查看部门权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewDepartVacation))
        {
            htParams.Add("Permit", Constants.Permit_ViewDepartVacation);
            htParams.Add("CurUserId", CurrentUserInfo.UserId);
        }
        //查看自己权限    
        else
        {
            htParams.Add("CurUserId", CurrentUserInfo.UserId);
        }
        
        try
        {
            //假期记录业务类
            Chenxin.Lib.Hr.BLL.Hr_VacationRecord bllVac = new Chenxin.Lib.Hr.BLL.Hr_VacationRecord();

            ////取得总记录数
            //intTotalCnt = bllVac.getTotalCnt(htParams);

            System.Data.DataSet ds = bllVac.GetModelListByCoditions(htParams);

            //intTotalCnt = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            
            strRet = retExecResult(Constants.ExecResult.Succ, "OK", GetJson(ds));

            return strRet;
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }

    //*******************************************
    //功能概要       ：转化为翻页结构体格式
    //参数説明       ：dtData 要转化的原始数据
    //               ：intShowRows 一页所要显示的条数
    //               ：intCurPage 当前的页码
    //               ：intTotal 数据总条数
    //返回值説明     ：翻页结构体
    //功能詳細       ：转化为翻页结构体格式
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-22 
    //*******************************************
    public Constants.Pagination GetJson(System.Data.DataSet dsData)
    {
        Constants.Pagination stuPag = new Constants.Pagination();
        System.Data.DataRow row;
        DateTime dtTempTime;
        
        System.Text.StringBuilder strData = new System.Text.StringBuilder();
        strData.Append("<table width=\"100%\" height=\"100%\" cellspacing=\"1\" class=\"tabsorternew\" id=\"report\">");
        strData.Append("<thead>");
        strData.Append("<tr>");
        strData.Append("<th width=\"40\">No.</th>");
        strData.Append("<th style=\"display: none;\" width=\"35\" >用户ID</th>");
        strData.Append("<th width=\"110\">用户名</th>");
        strData.Append("<th width=\"88\">部门名</th>");
        strData.Append("<th width=\"110\">申请日期</th>");
        strData.Append("<th width=\"110\" >休假开始日</th>");
        strData.Append("<th width=\"110\">休假结束日</th>");
        strData.Append("<th width=\"66\">休假天数</th>");
        strData.Append("<th width=\"112\" >假期类型</th>");
        strData.Append("<th style=\"display: none;\" width=\"81\">请假理由</th>");
        strData.Append("<th style=\"display: none;\" >部门ID</th>");
        strData.Append("</tr>");
        strData.Append("</thead>");
        strData.Append("<tbody>");

        for (int idxRow = 0; idxRow < dsData.Tables[0].Rows.Count; idxRow++)
        {
            row = dsData.Tables[0].Rows[idxRow];
            
            strData.AppendFormat("<tr id=\"{0}\" class=\"tr_houselist\">", row["RecordId"].ToString());

            strData.AppendFormat("<td>{0}</td>", (idxRow + 1).ToString());
            strData.AppendFormat("<td style=\"display: none;\">{0}</td>", row["UserId"].ToString());
            strData.AppendFormat("<td>{0}</td>", row["UserName"].ToString());
            strData.AppendFormat("<td>{0}</td>", row["DepartName"].ToString());
            dtTempTime = (DateTime)row["DateTime"];
            strData.AppendFormat("<td>{0}</td>", dtTempTime.Year.ToString().PadLeft(4, '0') + "-" + 
                                                 dtTempTime.Month.ToString().PadLeft(2, '0') + "-" + 
                                                 dtTempTime.Day.ToString().PadLeft(2, '0'));
            dtTempTime = (DateTime)row["StartTime"];
            strData.AppendFormat("<td>{0}</td>", dtTempTime.Year.ToString().PadLeft(4, '0') + "-" +
                                                 dtTempTime.Month.ToString().PadLeft(2, '0') + "-" +
                                                 dtTempTime.Day.ToString().PadLeft(2, '0'));
            dtTempTime = (DateTime)row["EndTime"];
            strData.AppendFormat("<td>{0}</td>", dtTempTime.Year.ToString().PadLeft(4, '0') + "-" +
                                                 dtTempTime.Month.ToString().PadLeft(2, '0') + "-" +
                                                 dtTempTime.Day.ToString().PadLeft(2, '0'));
            strData.AppendFormat("<td>{0}</td>", row["Days"].ToString());
            strData.AppendFormat("<td>{0}</td>", row["VacationType"].ToString());
            strData.AppendFormat("<td style=\"display: none;\">{0}</td>", row["Reason"].ToString());
            strData.AppendFormat("<td style=\"display: none;\">{0}</td>", row["DepartId"].ToString());
            
            strData.Append("</tr>");
        }

        strData.Append("</tbody></table>");

        stuPag.count = Convert.ToInt32(dsData.Tables[1].Rows[0][0]);
        stuPag.data = strData.ToString();

        return stuPag;
    }

    //*******************************************
    //功能概要       ：申请假期
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：申请假期
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-22
    //*******************************************
    private string addVac(HttpContext context)
    {
        //返回字符串
        string strRet;  
        try
        {        
            //假期记录业务类
            Chenxin.Lib.Hr.BLL.Hr_VacationRecord bllVac = new Chenxin.Lib.Hr.BLL.Hr_VacationRecord();
            //假期实体类
            Chenxin.Lib.Hr.Entity.Hr_VacationRecord entVac =
            (Chenxin.Lib.Hr.Entity.Hr_VacationRecord)Chenxin.Lib.Utility.JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.Hr.Entity.Hr_VacationRecord));
            
            entVac.RecordId = Guid.NewGuid();

            //entVac.DepartId = CurrentUserInfo.DepartId;
            //entVac.DepartName = CurrentUserInfo.DepartName;
            entVac.DateTime = DateTime.Now;

            bllVac.Add(entVac);
            
            //添加用户操作日志 2010-5-12 王玮 Add
            AddLog("" + CurrentUserInfo.UserName + "添加了一个假期申请:假期类型[" + entVac.VacationType + "]",
                    OperationType.OperationTypeComboList()["OprType_ApplyVacAdd"].ToString()); 
            
            strRet = retExecResult(Constants.ExecResult.Succ, "申请成功", "");
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
        }

        return strRet;
    }

    //*******************************************
    //功能概要       ：假期内容修改
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：假期内容修改
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-22
    //*******************************************
    private string editVac(HttpContext context)
    {
        //返回字符串
        string strRet;  
        try
        {
            //假期记录业务类
            Chenxin.Lib.Hr.BLL.Hr_VacationRecord bllVac = new Chenxin.Lib.Hr.BLL.Hr_VacationRecord();
            //假期实体类
            Chenxin.Lib.Hr.Entity.Hr_VacationRecord entVac = 
            (Chenxin.Lib.Hr.Entity.Hr_VacationRecord)Chenxin.Lib.Utility.JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.Hr.Entity.Hr_VacationRecord));
          
            //entVac.DepartId = CurrentUserInfo.DepartId;
            //entVac.DepartName = CurrentUserInfo.DepartName;
            entVac.DateTime = DateTime.Now;
         
            bllVac.Update(entVac);

            //添加用户操作日志 2010-5-12 王玮 Add
            AddLog("" + CurrentUserInfo.UserName + "编辑了一个假期申请:假期类型[" + entVac.VacationType + "]",
                    OperationType.OperationTypeComboList()["OprType_ApplyVacEdit"].ToString()); 
            
            strRet = retExecResult(Constants.ExecResult.Succ, "更新成功", "");
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
        }

        return strRet;
    }

    //*******************************************
    //功能概要       ：删除假期
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：删除假期
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-25
    //*******************************************
    private string delVal(HttpContext context)
    {
        //返回字符串
        string strRet;  
        try
        {
            //假期记录业务类
            Chenxin.Lib.Hr.BLL.Hr_VacationRecord bllVac = new Chenxin.Lib.Hr.BLL.Hr_VacationRecord();

            bllVac.Delete(new Guid(context.Request.Params["modelid"]));

            //添加用户操作日志 2010-5-12 王玮 Add
            AddLog("" + CurrentUserInfo.UserName + "删除了一个假期申请",
                    OperationType.OperationTypeComboList()["OprType_ApplyVacDel"].ToString()); 
            
            strRet = retExecResult(Constants.ExecResult.Succ, "删除成功", "");
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
        }

        return strRet;
    }

    //*******************************************
    //功能概要       ：年假天数
    //参数説明       ：context 请求的上下文
    //返回值説明     ：年假天数
    //功能詳細       ：获得年假天数
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-26
    //*******************************************
    private string checkAnnnualVac(HttpContext context)
    {
        //返回字符串
        string strRet;
        try
        {
            string strUserId = context.Request.Params["UserId"];
            string strRecordId = context.Request.Params["RecordId"];
            //假期业务类
            Chenxin.Lib.Hr.BLL.AnnnualVacation bllVac = new Chenxin.Lib.Hr.BLL.AnnnualVacation();

            Decimal totalCntOfAnnualVac = bllVac.getTotalCntOfAnnnualVac(strUserId);
            Decimal usedCntOfAnnualVac = bllVac.getUsedCntOfAnnnualVac(strUserId, strRecordId);

            strRet = retExecResult(Constants.ExecResult.Succ, "", totalCntOfAnnualVac - usedCntOfAnnualVac);
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
        }
        
        return strRet;
    }

    //*******************************************
    //功能概要       ：获得假期类型
    //参数説明       ：context 请求的上下文
    //返回值説明     ：假期列表所需的json数据
    //功能詳細       ：获得假期类型
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-24 
    //*******************************************
    private string getControlDeparts(HttpContext context)
    {
        string strRet;
        try
        {

            System.Data.DataSet ds = CurrentUserInfo.GetUserControledDeparts();

            //生成JSON字符串
            System.Text.StringBuilder strHtml = new System.Text.StringBuilder();

            strHtml.Append("{txt:'请选择',val:'请选择'}");
            foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
            {
                strHtml.Append(", { txt: '" + dr["DepartName"] + "', val: '" + dr["DepartId"] + "' }");
            }

            strRet = string.Format("{{success:'1',msg:'成功',data:{{items:[{0}]}}}}", strHtml.ToString());
            return strRet;
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }
}